﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using System.Drawing.Printing;
using DevExpress.XtraGrid.Columns;
using System.IO;

namespace KasaTakip
{
    public partial class frmViewer : XtraForm
    {
        public object DataSource { get; set; }

        public frmViewer()
        {
            InitializeComponent();
        }

        private void frmViewer_Load(object sender, EventArgs e)
        {
            if (DataSource != null)
            {
                this.gridControl1.DataSource = this.DataSource;
                gridView1.ColumnPanelRowHeight = 60;
                foreach (GridColumn item in gridView1.Columns)
                {
                    item.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                }
                gridView1.BestFitColumns();
            }

            if (File.Exists(Application.StartupPath + "\\Rapor.xml"))
            {
                gridView1.RestoreLayoutFromXml(Application.StartupPath + "\\Rapor.xml");
            }
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                string bilgi = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Bilgi"]);
                if (bilgi.IndexOf(':') > -1)
                {
                    e.Appearance.BackColor = Color.Salmon;
                    e.Appearance.BackColor2 = Color.SeaShell;
                }
                else if (bilgi.IndexOf('-') > -1)
                {
                    e.Appearance.BackColor = Color.DeepSkyBlue;
                    e.Appearance.BackColor2 = Color.LightCyan;
                }
            }
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Yatay yazdırılsın mı ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == System.Windows.Forms.DialogResult.Yes)
            {
                Print(true);
            }
            else
            {
                Print(false);
            }
        }

        private void Print(bool yatay)
        {
            DevExpress.XtraPrinting.PrintingSystem printingSystem1 = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();
            try
            {
                printLink.Component = gridControl1;
                printLink.CreateDocument(printingSystem1);
                printingSystem1.PageSettings.Landscape = true;

                XtraPageSettings pageSettings = printingSystem1.PageSettings;

                // Specify the paper kind and page orientation.
                pageSettings.PaperKind = PaperKind.A4;
                pageSettings.Landscape = yatay;

                // Specify margins.
                pageSettings.LeftMargin = 10;
                pageSettings.RightMargin = 10;
                pageSettings.TopMargin = 10;
                pageSettings.BottomMargin = 10;

                printingSystem1.PrintDlg();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                printingSystem1.Dispose();
                printLink.Dispose();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DevExpress.XtraPrinting.PrintingSystem printingSystem1 = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();
            try
            {
                printLink.Component = gridControl1;
                printLink.CreateDocument(printingSystem1);
                printingSystem1.PageSettings.Landscape = true;
                XtraPageSettings pageSettings = printingSystem1.PageSettings;

                // Specify the paper kind and page orientation.
                pageSettings.PaperKind = PaperKind.Letter;
                pageSettings.Landscape = true;

                // Specify margins.
                pageSettings.LeftMargin = 20;
                pageSettings.RightMargin = 20;
                pageSettings.TopMargin = 20;
                pageSettings.BottomMargin = 20;

                printingSystem1.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                printingSystem1.Dispose();
                printLink.Dispose();
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            if (File.Exists(Application.StartupPath + "\\Rapor.xml"))
            {
                File.Delete(Application.StartupPath + "\\Rapor.xml");
            }
            gridView1.SaveLayoutToXml(Application.StartupPath + "\\Rapor.xml");
        }
    }
}